package cn.flying.base.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 字符串处理工具类
 * @author: lvyong
 * @date: 2022年04月02日 21:43
 * @version: 1.0
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    public StringUtils() {
    }

    /***
     * 下划线命名转为驼峰命名
     * @param str
     */
    public static String underlineToHump(String str) {
        StringBuilder result = new StringBuilder();
        String[] splits = str.split("_");
        for (String s : splits) {
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToUnderline(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                stringBuilder.append("_").append(str.charAt(i));
            } else {
                stringBuilder.append(str.charAt(i));
            }
        }
        return stringBuilder.toString().toLowerCase();
    }

    /**
     * 左拼接 0字符
     * @param str
     * @param strLength
     * @return
     */
    public static String padLeft(String str, int strLength) {
        return padLeft(str, '0', strLength);
    }

    /**
     * 左拼接
     * @param str
     * @param c
     * @param strLength
     * @return
     */
    public static String padLeft(String str, char c, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append(c).append(str);
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    /**
     * 右拼接 0字符
     * @param str
     * @param strLength
     * @return
     */
    public static String padRight(String str, int strLength) {
        return padRight(str, '0', strLength);
    }

    /**
     * 右拼接
     * @param str
     * @param c
     * @param strLength
     * @return
     */
    public static String padRight(String str, char c, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append(str).append("0");
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }

    /**
     * List 转 Array
     * @param list
     * @return
     */
    public static String[] toStringArr(List<String> list) {
        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); ++i) {
            result[i] = (String) list.get(i);
        }
        return result;
    }

    /**
     * Array 转List
     * @param strings
     * @return
     */
    public static List<String> toStringList(String[] strings) {
        if (strings == null) {
            return null;
        } else {
            List<String> list = new ArrayList();
            for (int i = 0; i < strings.length; ++i) {
                list.add(strings[i]);
            }
            return list;
        }
    }
}
